package com.novellotus.iov.carvice.activity;

import android.Manifest;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.novellotus.httploadingview.LoadingView;
import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.adapter.TaskDetailAdapter;
import com.novellotus.iov.carvice.bean.RxTaskDetail;
import com.novellotus.iov.carvice.retrofit.BaseEntity;
import com.novellotus.iov.carvice.retrofit.BaseObserver;
import com.novellotus.iov.carvice.retrofit.RetrofitFactory;
import com.novellotus.iov.carvice.retrofit.RxSchedulers;
import com.novellotus.iov.carvice.utils.MyUtils;
import com.novellotus.iov.carvice.utils.MyViewUtils;
import com.novellotus.iov.carvice.utils.SharedPreferencesUtils;
import com.tbruyelle.rxpermissions2.Permission;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.functions.Consumer;

/**
 * 售后详情页面
 * */
public class TaskDetailActivity extends BaseActivity implements LoadingView.OnBtnClickListener, TaskDetailAdapter.TaskDetailInterface {

    @BindView(R.id.tv_tittle)
    TextView tv_tittle;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_plate)
    TextView tv_plate;
    @BindView(R.id.tv_vin)
    TextView tv_vin;
    @BindView(R.id.tv_statue)
    TextView tv_statue;
    @BindView(R.id.loadingView)
    LoadingView loadingView;
    @BindView(R.id.rlt_top)
    RelativeLayout rlt_top;
    String fid = "", vin = "", plate = "", statue = "";

    //1.维修记录：status:已取消={0} 未处理={1} 处理中={2,3,4,5} 已完成={6}
    @Override
    protected void initContentView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_task_detail);
    }

    @Override
    public void init() {
        //4.4系统下移一个状态栏高度
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
            rlt_top.setPadding(0, MyUtils.getStatusBarHeight(getApplicationContext()), 0, 0);
        }
        tv_tittle.setText("维修详情");
        fid = getIntent().getExtras().getString("fid");
        plate = getIntent().getExtras().getString("plate");
        vin = getIntent().getExtras().getString("vin");
        tv_plate.setText(plate);
        tv_vin.setText(vin);

        recyclerView.setLayoutManager(MyViewUtils.getLinearLayoutManager(this));
        loadingView.setLoadingModel(LoadingView.MODEL_ALERT);
        loadingView.mWarnView.setTextColor(Color.parseColor("#333333"));
        loadingView.setOnBtnClickListener(this);
        getTaskList();
    }

    public void getTaskList() {
        Observable<BaseEntity<RxTaskDetail>> observable = RetrofitFactory.getInstance().taskDetail(fid, SharedPreferencesUtils.getKey(this)[0]);
        observable.compose(RxSchedulers.<BaseEntity<RxTaskDetail>>compose())
                .subscribe(new BaseObserver<RxTaskDetail>(this) {

                    @Override
                    public void onSuccess(RxTaskDetail response) {
                        loadingView.success();
                        recyclerView.setAdapter(new TaskDetailAdapter(response.getProgress(), TaskDetailActivity.this, TaskDetailActivity.this));
                        tv_statue.setVisibility(View.VISIBLE);
                        switch (response.getStatus()) {
                            case "0":
                                tv_statue.setText("已取消");
                                break;
                            case "1":
                                tv_statue.setText("未处理");
                                break;
                            case "2":
                            case "3":
                            case "4":
                            case "5":
                                tv_statue.setText("处理中");
                                break;
                            case "6":
                                tv_statue.setText("已完成");
                                break;
                        }

                    }

                    @Override
                    public void onFail(int code, String msg) {
                        loadingView.empty(msg, true);
                    }

                    @Override
                    public void onError(String msg) {
                        loadingView.empty(msg, true);
                    }
                });
    }

    @OnClick(R.id.img_back)
    void back() {
        finish();
    }

    @OnClick(R.id.btn_confirm)
    void openConfirm() {
        openActivity(ConfirmActivity.class);
    }

    @Override
    public void onReload() {
        getTaskList();
    }

    @Override
    public void call(final String phone) {
        if (MyUtils.isMobile(phone) || MyUtils.isPhone(phone)) {
            Observable.create(new ObservableOnSubscribe<Object>() {
                @Override
                public void subscribe(ObservableEmitter<Object> e) throws Exception {
                    e.onNext("");
                }
            }).compose(RxSchedulers.lifecycle(this)).throttleFirst(1, TimeUnit.SECONDS)
                    .compose(rxPermissions.ensureEach(Manifest.permission.CALL_PHONE)).subscribe(new Consumer<Permission>() {
                @Override
                public void accept(Permission permission) throws Exception {
                    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            });
        }

    }
}
